<?php
require ('../include/init.inc.php');
$user_id = $user_name = $user_phone = $user_password = $user_group = $user_sex = $real_name = $user_id_type
= $user_num = $user_duty = $user_register_time = $user_photo = '';
extract ( $_REQUEST, EXTR_IF_EXISTS );

Common::checkParam($user_id);
$user = FamilyGroupUser::getUserById ( $user_id );
if(empty($user)){
	Common::exitWithError(ErrorMessage::USER_NOT_EXIST,"panel/family_group_user.php");
}
if (Common::isPost ()) {
	
    $pic = $_FILES['user_photo']['tmp_name'];
    $upload_ret = false;
    if($pic){
        // 上传的路径
        $photoDir = $_SERVER['DOCUMENT_ROOT']."/photos/FamilyGroup";
        // 创建文件夹
        if(!file_exists($photoDir)){
            mkdir($photoDir, 0777);
        }
        // 用时间戳来保存图片，防止重复
        $timeNow = time();
        $targetFile = $photoDir . '/' . $timeNow.'.jpg';
        $sqlFile = "/photos/FamilyGroup/".$timeNow.'.jpg';
        echo $targetFile."<br / >";
        // 将临时文件 移动到我们指定的路径，返回上传结果
        $upload_ret = move_uploaded_file($pic, $targetFile) ? true : false;
    }
    
	if(($user_id !=1 && $user_group <= 0)){
		
		OSAdmin::alert("error",ErrorMessage::NEED_PARAM);
	}else{
		
		$update_data = array ('user_id' => $user_id, 'user_name' => $user_name,
		    'user_group' => $user_group,
		    'user_sex' => $user_sex, 'real_name' => $real_name, 'user_id_type' => $user_id_type, 'user_num' => $user_num
		    , 'user_duty' => $user_duty, 'user_register_time' => $user_register_time
		    , 'user_phone' => $user_phone
		);
		if($user_id > 1 ){
			  $update_data["user_group"]=$user_group;
		}
		
		if($upload_ret == true){
		    $update_data = array_merge ( $update_data, array ('user_photo' => $sqlFile ));
		}
		
		if (! empty ( $password )) {
			$update_data = array_merge ( $update_data, array ('password' => md5 ( $password )));
		}
		$result = FamilyGroupUser::updateUser ( $user_id,$update_data );
		if ($result>=0) {
			$current_user=UserSession::getSessionInfo();
			$ip = Common::getIp();
			$update_data['ip']=$ip;
			SysLog::addLog ( UserSession::getUserName(), 'MODIFY', 'User' , $user_id, json_encode($update_data) );
			Common::exitWithSuccess ('更新完成','panel/family_group_user.php');
		} else {
			
			OSAdmin::alert("error");
		}
	}
}

$group_options=UserGroup::getGroupForOptions();

Template::assign ( 'client_info', $user );
Template::assign ( 'group_options', $group_options );
Template::display ( 'panel/family_group_user_modify.tpl' );